<?php
namespace app\common\service\shop;

use app\common\model\shop\ozon\OzonGoodsModel;
use app\common\model\shop\self\SelfGoodsModel;
use app\common\model\shop\ShopsModel;
use app\common\service\BaseService;
use app\common\service\CrmebService;
use app\common\service\shop\Ozon;
use think\admin\Model;

class SyncGoodService extends BaseService
{
    /**
     * 商品模型
     *
     * @var string[]
     */
    public static $goods_model = [
        'ozon' => OzonGoodsModel::class,
        'self' => SelfGoodsModel::class,
    ];

    /**
     * 服务类数组
     *
     * @var string[]
     */
    public static $platform_service = [
        'ozon' => Ozon::class,
        'self'  => CrmebService::class
    ];

    /**
     * 推送商品到对应的平台
     * @param array $goods_ids 要推送的商品id数组
     * @param string $flag   三方平台的标识
     * @return array
     */
    public function pushGoods (array $goods_ids, string $flag)
    {
        $ret = ['code' => 0, 'msg' => ''];
        // 获得对应的 模型
        $goodsModel = $this->getModel($flag);
        // 获得商品信息
        $goods_list = $goodsModel->whereIn('id', $goods_ids)->select()->toArray();
        if(empty($goods_list)){
            $ret['msg'] = '推送失败:没有找到对应的商品';
            return $ret;
        }
        $max_co = 30;
        if(count($goods_list) > $max_co){
            $ret['msg'] = '推送失败:商品同步数量不能超过'.$max_co.'个';
            return $ret;
        }
        // 获得对应店铺的配置信息
        $shop_ids = array_column($goods_list, 'shop_id');
        $shop_data   = ShopsModel::mk()->where('id', 'in', $shop_ids)->column('id,client_id,api_key,currency_id,url', 'id');
        if(empty($shop_data)){
            $ret['msg'] = '推送失败:没有找到对应的店铺';
            return $ret;
        }
        // 获得 商品相关的数据
        $data       = $goodsModel->getPushGoodsData($goods_list, $goods_ids, $shop_data);
        if(!empty($data['error'])){
            $ret['msg'] = '推送失败:'.implode(';', $data['error']);
            return $ret;
        }
        if(empty($data['success'])){
            $ret['msg'] = '推送失败:没有需要推送的商品';
            return $ret;
        }
        foreach ($data['success'] as $shop_id => $value) {
            $shop_obj = !empty($shop_data[$shop_id]) ? $shop_data[$shop_id] : [];
            // 获得对应的service
            $platform_service = $this->getPlatformService($flag, $shop_obj);
            $push_ret = $goodsModel->actPushGoods($platform_service, $value, $shop_obj);
            if(empty($push_ret['code']) || ($push_ret['code'] != 1)){
                $ret['msg'] = '推送失败:'.$push_ret['msg'];
                return $ret;
            }
        }
        $ret = ['code' => 1, 'msg' => '推送成功'];
        return $ret;
    }


    /**
     * 获得对应的model
     * @param string $flag 三方平台的标识
     * @return Model
     */
    protected function getModel(string $flag) : Model
    {
        return new self::$goods_model[$flag];
    }


    /**
     * 获得对应的平台service
     *
     * @return object
     */
    private function getPlatformService ($flag, $config) : object
    {
        $platform_service = new self::$platform_service[$flag]($config);
        if(method_exists($platform_service, 'setConfig')){
            $platform_service->setConfig($config);
        }
        return $platform_service;
    }

    /**
     * 推送商品库存
     * @param array $goods_ids
     * @param string $flag
     * @Description
     * @author wangqinghui
     * @date 2024-12-26 16:03
     * @return array
     */
    public function pushGoodsStock(array $goods_ids, string $flag): array
    {
        $ret = ['code' => 0, 'msg' => ''];
        $goods_model = $this->getModel($flag);
        // 获取商品信息
        $field = 'id,shop_id';
        $goods_list = $goods_model->field($field)->whereIn('id', $goods_ids)->select()->toArray();
        if(empty($goods_list)){
            $ret['msg'] = '没有找到对应的商品';
            return $ret;
        }
        $maxNum = 30;
        if(count($goods_list) > $maxNum){
            $ret['msg'] = '同步商品数量不能超过'.$maxNum.'个';
            return $ret;
        }

        // 获取商品sku
        $sku_ret = $goods_model->getStockSkuList($goods_ids);
        if(!empty($sku_ret['error'])){
            $ret['msg'] = implode('<br>', $sku_ret['error']);
            return $ret;
        }
        if(empty($sku_ret['success']['stock'])){
            $ret['msg'] = '没有需要同步库存的商品';
            return $ret;
        }
        // 获取店铺信息
        $shop_ids = array_column($goods_list, 'shop_id');
        $map = [
            ['id', 'in', $shop_ids]
        ];
        $shop_list = ShopsModel::mk()->where($map)->column('id,client_id,api_key,url', 'id');
        if(empty($shop_list)){
            $ret['msg'] = '没有找到对应的店铺';
            return $ret;
        }
        $sku_stock_data = $sku_ret['success']['stock'];
        $sku_stock_ids = $sku_ret['success']['stock_id'];
        foreach ($sku_stock_data as $shop_id => $stock_data) {
            $shop_obj = !empty($shop_list[$shop_id]) ? $shop_list[$shop_id] : [];
            // 获得对应的service
            $platform_service = $this->getPlatformService($flag, $shop_obj);
            $push_ret = $goods_model->actPushGoodsStock($platform_service, $stock_data, $sku_stock_ids);
            if(!empty($push_ret['error'])){
                $ret['msg'] = '推送失败:'.implode('<br>', $push_ret['error']);
                return $ret;
            }
        }
        $ret = ['code' => 1, 'msg' => '同步库存成功'];
        return $ret;
    }
}